package com.yixun.qfbao.hander;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yixun.qfbao.api.service.AgentRuleService;
import com.yixun.qfbao.apidto.GetAgentRuleResDto;
import com.yixun.qfbao.apidto.base.CommonReqDTO;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.Company;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.model.dto.CompanyDto;
import com.yixun.qfbao.service.CompanyService;
import com.yixun.qfbao.util.StringUtil;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.GeneralReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;

/**
 * @author zilong.liu
 * @date 2020-12-16
 *
 * desc:
 *     	根据经纪人的手机号以及总代companyUuid获取对应的经济公司规则
 */
@Service("getAgentRuleHander")
public class GetAgentRuleHander extends CommonAbstractHander{

	@Autowired
	AgentRuleService agentRuleService;
	
	@Autowired
	CompanyService companyService;
	
	@Autowired
	LoginUtils loginUtils;
	
	@SuppressWarnings("rawtypes")
	@Override
	public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
		@SuppressWarnings("unchecked")
		GeneralReqVo<CommonReqDTO> reqVo = (GeneralReqVo<CommonReqDTO>) reqDto;
		
		String userId = reqVo.getParams().getUserId();
		BaseResponseDTO<GetAgentRuleResDto> res = new BaseResponseDTO<>();
		PartnerInfo partnerInfo = loginUtils.getUser(userId);
		if(partnerInfo == null) {
			res.setResult(ResultEnum.FAIL.getNo());
			res.setErrorMsg("未登录");
			return res;
		}
		
		String companyUuid = reqVo.getParams().getCompanyUuid();
		if(StringUtil.isNotNull(companyUuid)) {
			CompanyDto entity = new CompanyDto();
			entity.setCompanyUuid(companyUuid);
			Company company = companyService.selectByDto(entity);
			if(company == null) {
				res.setResult(ResultEnum.FAIL.getNo());
				res.setErrorMsg("companyUuid 异常");
				return res;
			}
			GetAgentRuleResDto getAgentRuleResDto = agentRuleService.getAgentRule(partnerInfo.getHigherid(), company.getEnableWeappMsg());
			getAgentRuleResDto.setRuleDisplayFlag("1".equals(company.getEnableWeappMsg()));
			res.setResData(getAgentRuleResDto);
			res.setResult(getAgentRuleResDto.getResult());
			res.setErrorMsg(getAgentRuleResDto.getResultMsg());
			return res;
		}
		res.setResult(ResultEnum.FAIL.getNo());
		res.setErrorMsg("companyUUID传入异常");
		return res;
		
	}

}
